package com.jlu.controller;

import com.jlu.entity.Product;
import com.jlu.vos.CollectionVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("product")
public class ProductController {

  @Value("${server.port}")
  private int port;

  @GetMapping("productList")
  public String product(){
    return "product is ok !!! 端口号是：" + port;
  }

  @GetMapping("list")
  public String list(){
    return "list is ok!!! 端口号是：" + port;
  }

//  定义一个接收零散参数类型的方法 queryString
  @GetMapping("test")
  public String test(String name,Integer age){
    return "test is ok" + name + "  " + age;
  }

//  定义一个接收零散参数类型的方法 路径传参
  @GetMapping("test1")
  public String test1(Integer id,String name){
    return "test1 is ok " + id + " " + name;
  }

//  定义一个接收对象类型的接口
  @PostMapping("test2")
  public String test2(@RequestBody Product product){
    return "test2 is ok !!!" + product;
  }

//  定义一个接受数组类型的接口
  @GetMapping("test3")
  public String test3(@RequestParam("ids") String ids[]){
    for (String id : ids) {
      System.out.println(id);
    }
    return "test3 is ok !!! ";
  }
//  定义一个接收集合对象的接口
  @GetMapping("test4")
  public String test4(CollectionVo collectionVo){
    for (String s : collectionVo.getIds()) {
      System.out.println(s);
    }
    return "test4 is ok";
  }

//  定义一个接口接收id参数，返回一个product对象
  @GetMapping("product/{id}")
  public Product test5(@PathVariable("id")Integer id) throws InterruptedException {
    Thread.sleep(2000);
    return new Product(id,"蓝光",23.23,new Date());
  }

//  定义一个接口接受id参数，返回一个list
  @GetMapping("products")
  public List<Product> findByCategoryId(@RequestParam("id")Integer id){
    List<Product> list = new ArrayList<Product>();
    list.add(new Product(1,"碟",23.23,new Date()));
    list.add(new Product(2,"碟3",23.23,new Date()));
    return list;

  }

//  以id分页查询商品信息和总条数
  @GetMapping("productss")
  public Map<String,Object> findByCategoryIdAndPage(@RequestParam("id")Integer id,@RequestParam("rows")Integer rows,@RequestParam("page")Integer page){
    Map<String, Object> map = new HashMap<String, Object>();
    List<Product> list = new ArrayList<Product>();
    list.add(new Product(1,"碟",23.23,new Date()));
    list.add(new Product(2,"碟3",23.23,new Date()));
    map.put("rows",list);
    map.put("total",100);
    return map;
  }
}
